//
//  Account.m
//  RRQS
//
//  Created by kyjun on 16/3/30.
//
//

#import "Account.h"
#import "AccountDetail.h"
#import "ATM.h"

@interface Account ()
@property(nonatomic,strong) UITableView* tableView;
@property(nonatomic,strong) UIView* headerView;
@property(nonatomic,strong) UIImageView* iconMoney;
@property(nonatomic,strong) UILabel* labelTitle;
@property(nonatomic,strong) UILabel* labelMoney;
@property(nonatomic,strong) UIButton* btnDeail;
@property(nonatomic,strong) UIButton* btnAppCash;

@end

@implementation Account

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = theme_default_color;
    self.title = @"账户余额";
    [self layoutUI];
    [self layoutConstraints];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(queryData) name:NotifciationReLoadUserInfo object:nil];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.labelMoney.text = [NSString stringWithFormat:@"%.2f",[self.Identity.userInfo.account floatValue]];
}

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark =====================================================  user interface layout
-(void)layoutUI{
     [self.view addSubview:self.btnAppCash];
    [self.view addSubview:self.tableView];
    self.headerView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 100);
    self.tableView.tableHeaderView = self.headerView;
    [self.headerView addSubview:self.iconMoney];
    [self.headerView addSubview:self.labelTitle];
    [self.headerView addSubview:self.labelMoney];
    [self.headerView addSubview:self.btnDeail];
}

-(void)layoutConstraints{
    self.tableView.translatesAutoresizingMaskIntoConstraints = NO;
    self.iconMoney.translatesAutoresizingMaskIntoConstraints = NO;
    self.labelTitle.translatesAutoresizingMaskIntoConstraints = NO;
    self.labelMoney.translatesAutoresizingMaskIntoConstraints  = NO;
    self.btnDeail.translatesAutoresizingMaskIntoConstraints = NO;
    self.btnAppCash.translatesAutoresizingMaskIntoConstraints = NO;
    
    [self.btnAppCash addConstraint:[NSLayoutConstraint constraintWithItem:self.btnAppCash attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:50.f]];
    [self.btnAppCash addConstraint:[NSLayoutConstraint constraintWithItem:self.btnAppCash attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:SCREEN_WIDTH]];
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.btnAppCash attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.f]];
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.btnAppCash attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0.f]];
    
    [self.tableView addConstraint:[NSLayoutConstraint constraintWithItem:self.tableView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:SCREEN_WIDTH]];
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.tableView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.topLayoutGuide attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.f]];
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.tableView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0.f]];
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.tableView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.btnAppCash attribute:NSLayoutAttributeTop multiplier:1.0 constant:0.f]];
    
    [self.iconMoney addConstraint:[NSLayoutConstraint constraintWithItem:self.iconMoney attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:75.f]];
    [self.iconMoney addConstraint:[NSLayoutConstraint constraintWithItem:self.iconMoney attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:75.f]];
    [self.headerView addConstraint:[NSLayoutConstraint constraintWithItem:self.iconMoney attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.headerView attribute:NSLayoutAttributeTop multiplier:1.0 constant:100.f]];
    [self.headerView addConstraint:[NSLayoutConstraint constraintWithItem:self.iconMoney attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.headerView attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0.f]];
    
    [self.labelTitle addConstraint:[NSLayoutConstraint constraintWithItem:self.labelTitle attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:SCREEN_WIDTH]];
    [self.labelTitle addConstraint:[NSLayoutConstraint constraintWithItem:self.labelTitle attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:60.f]];
    [self.headerView addConstraint:[NSLayoutConstraint constraintWithItem:self.labelTitle attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.iconMoney attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.f]];
    [self.headerView addConstraint:[NSLayoutConstraint constraintWithItem:self.labelTitle attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.headerView attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0.f]];
    
    [self.labelMoney addConstraint:[NSLayoutConstraint constraintWithItem:self.labelMoney attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:SCREEN_WIDTH]];
    [self.labelMoney addConstraint:[NSLayoutConstraint constraintWithItem:self.labelMoney attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:60.f]];
    [self.headerView addConstraint:[NSLayoutConstraint constraintWithItem:self.labelMoney attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.labelTitle attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.f]];
    [self.headerView addConstraint:[NSLayoutConstraint constraintWithItem:self.labelMoney attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.headerView attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0.f]];
    
    [self.btnDeail addConstraint:[NSLayoutConstraint constraintWithItem:self.btnDeail attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:SCREEN_WIDTH]];
    [self.btnDeail addConstraint:[NSLayoutConstraint constraintWithItem:self.btnDeail attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:60.f]];
    [self.headerView addConstraint:[NSLayoutConstraint constraintWithItem:self.btnDeail attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.labelMoney attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.f]];
    [self.headerView addConstraint:[NSLayoutConstraint constraintWithItem:self.btnDeail attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.headerView attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0.f]];
    
    
}

#pragma mark =====================================================  DataSource
-(void)queryData{
    AFHTTPRequestOperationManager* mgr = [AFHTTPRequestOperationManager manager];
    NSDictionary* arg = @{@"ince":@"get_emp_info",@"emp_id":self.Identity.userInfo.userID};
    [mgr POST:kHost parameters:arg success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        [WMHelper outPutJsonString:responseObject];
        NSInteger flag = [[responseObject objectForKey:@"status"]integerValue];
        if(flag == 1){
            NSDictionary* emptyDict = [responseObject objectForKey:@"info"];
            if(![WMHelper isNULLOrnil:emptyDict]){
                MMember* item = self.Identity.userInfo;
                item.dataLine = [emptyDict objectForKey:@"dataline"];
                item.status = [emptyDict objectForKey:@"status"];
                item.account = [emptyDict objectForKey:@"account"];
                item.lat = [emptyDict objectForKey:@"lat"];
                item.lng = [emptyDict objectForKey:@"lng"];
                item.close = [emptyDict objectForKey:@"close"];
                
                Boolean flag= [NSKeyedArchiver archiveRootObject:item toFile:[WMHelper archiverPath]];
                if(flag){
                    self.labelMoney.text = self.Identity.userInfo.account;
                }else
                    NSLog(@"%@",@"归档失败!");
            }
            
        }else{
            [self alertHUD:[responseObject objectForKey:@"fail"]];
        }

    } failure:^(AFHTTPRequestOperation * _Nonnull operation, NSError * _Nonnull error) {
        [self alertHUD:@"网络异常"];
    }];
    
}

#pragma mark =====================================================  SEL
-(IBAction)detailTouch:(id)sender{
    AccountDetail* controller = [[AccountDetail alloc]init];
    [self.navigationController pushViewController:controller animated:YES];
}
-(IBAction)appCashTouch:(id)sender{
    ATM* controller = [[ATM alloc]init];
    [self.navigationController pushViewController:controller animated:YES];
}

#pragma mark =====================================================  property package 

-(UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc]init];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tableView;
}

-(UIView *)headerView{
    if(!_headerView){
        _headerView = [[UIView alloc]init];
        _headerView.backgroundColor = [UIColor whiteColor];
        
    }
    return _headerView;
}

-(UIImageView *)iconMoney{
    if(!_iconMoney){
        _iconMoney = [[UIImageView alloc]init];
        [_iconMoney setImage:[UIImage imageNamed:@"icon-cash"]];
    }
    return _iconMoney;
}

-(UILabel *)labelTitle{
    if(!_labelTitle){
        _labelTitle = [[UILabel alloc]init];
        _labelTitle.text = @"余额总数(元)";
        _labelTitle.textAlignment = NSTextAlignmentCenter;
        _labelTitle.textColor = [UIColor colorWithRed:64/255.f green:64/244.f blue:64/255.f alpha:1.0];
        _labelTitle.font = [UIFont systemFontOfSize:17.f];
    }
    return _labelTitle;
}

-(UILabel *)labelMoney{
    if(!_labelMoney){
        _labelMoney = [[UILabel alloc]init];
        _labelMoney.textAlignment = NSTextAlignmentCenter;
        _labelMoney.textColor = [UIColor colorWithRed:248/255.f green:162/244.f blue:48/255.f alpha:1.0];
        _labelMoney.font = [UIFont boldSystemFontOfSize:24.f];
    }
    return _labelMoney;
}

-(UIButton *)btnDeail{
    if(!_btnDeail){
        _btnDeail = [UIButton buttonWithType:UIButtonTypeCustom];
        [_btnDeail setTitleColor:[UIColor colorWithRed:248/255.f green:162/244.f blue:48/255.f alpha:1.0] forState:UIControlStateNormal];
        _btnDeail.titleLabel.font =[UIFont systemFontOfSize:17.f];
        [_btnDeail setTitle:@"查看余额明细" forState:UIControlStateNormal];
        _btnDeail.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
        [_btnDeail addTarget:self action:@selector(detailTouch:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _btnDeail;
}

-(UIButton *)btnAppCash{
    if(!_btnAppCash){
        _btnAppCash = [UIButton buttonWithType:UIButtonTypeCustom];
        [_btnAppCash setTitleColor:[UIColor colorWithRed:64/255.f green:64/244.f blue:64/255.f alpha:1.0] forState:UIControlStateNormal];
        [_btnAppCash setTitle:@"申请提现" forState:UIControlStateNormal];
        _btnAppCash.titleLabel.font =[UIFont systemFontOfSize:17.f];
        CALayer* border = [[CALayer alloc]init];
        border.frame = CGRectMake(0, 0.f, SCREEN_WIDTH, 1.f);
        border.backgroundColor = [UIColor colorWithRed:230/255.f green:230/255.f blue:230/255.f alpha:1.0].CGColor;
        [_btnAppCash.layer addSublayer:border];
        [_btnAppCash addTarget:self action:@selector(appCashTouch:) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return _btnAppCash;
}

@end
